package com.zxgj.datadirect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxgj.datadirect.entity.DataDirectExchangeTaskLog;
import com.zxgj.datadirect.mapper.DataDirectExchangeTaskLogMapper;
import com.zxgj.datadirect.service.DataDirectExchangeTaskLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据直达交换任务日志服务实现类
 * 实现DataDirectExchangeTaskLogService接口，封装日志相关业务逻辑
 */
@Service
public class DataDirectExchangeTaskLogServiceImpl extends ServiceImpl<DataDirectExchangeTaskLogMapper, DataDirectExchangeTaskLog> 
        implements DataDirectExchangeTaskLogService {

    @Resource
    private DataDirectExchangeTaskLogMapper exchangeTaskLogMapper;

    /**
     * 实现：根据任务ID查询日志列表
     */
    @Override
    public List<DataDirectExchangeTaskLog> listByTaskId(Long taskId) {
        LambdaQueryWrapper<DataDirectExchangeTaskLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataDirectExchangeTaskLog::getTaskId, taskId)
                .orderByDesc(DataDirectExchangeTaskLog::getOperateTime); // 按操作时间倒序，最新日志在前
        return exchangeTaskLogMapper.selectList(wrapper);
    }

    /**
     * 实现：根据任务ID和时间范围查询日志
     */
    @Override
    public List<DataDirectExchangeTaskLog> listByTaskIdAndTimeRange(Long taskId, String startTime, String endTime) {
        LambdaQueryWrapper<DataDirectExchangeTaskLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataDirectExchangeTaskLog::getTaskId, taskId);

        // 若传入开始时间，添加大于等于条件
        if (StringUtils.hasText(startTime)) {
            wrapper.ge(DataDirectExchangeTaskLog::getOperateTime, startTime);
        }
        // 若传入结束时间，添加小于等于条件
        if (StringUtils.hasText(endTime)) {
            wrapper.le(DataDirectExchangeTaskLog::getOperateTime, endTime);
        }

        wrapper.orderByDesc(DataDirectExchangeTaskLog::getOperateTime);
        return exchangeTaskLogMapper.selectList(wrapper);
    }

    /**
     * 实现：批量保存交换任务日志
     */
    @Override
    public int saveBatchLogs(List<DataDirectExchangeTaskLog> logList) {
        if (logList == null || logList.isEmpty()) {
            return 0;
        }
        // 调用MyBatis-Plus批量插入方法，若需自定义逻辑可在此扩展（如补充创建时间、操作人等）
        return exchangeTaskLogMapper.insertBatchSomeColumn(logList); 
        // 注：insertBatchSomeColumn需MyBatis-Plus 3.3.0+版本，若版本较低可改用saveBatch(logList)
    }

    /**
     * 实现：根据日志ID删除指定日志
     */
    @Override
    public boolean removeLogById(Long logId) {
        if (logId == null) {
            return false;
        }
        // 物理删除：直接删除数据库记录
        int deleteCount = exchangeTaskLogMapper.deleteById(logId);
        // 若需逻辑删除（如通过is_deleted字段标记），需在实体类加@TableLogic注解，并调整为update操作
        // LambdaQueryWrapper<DataDirectExchangeTaskLog> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(DataDirectExchangeTaskLog::getId, logId).set(DataDirectExchangeTaskLog::getIsDeleted, 1);
        // int deleteCount = exchangeTaskLogMapper.update(null, wrapper);
        return deleteCount > 0;
    }
}